package com.dearbinge.data.service.impl.collect;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.collect.CommodityCollectService;
import com.dearbinge.data.dao.CommodityMessageMapper;
import com.dearbinge.data.dao.collect.CommodityCollectMapper;
import com.dearbinge.data.pojo.collect.Collect;
import com.dearbinge.data.pojo.collect.vo.CommodityCollectVO;
import com.dearbinge.data.pojo.commodity.CommodityMessage;
import com.dearbinge.data.pojo.commodity.vo.CommodityMessageResponseVO;
import com.dearbinge.utils.BeanMapperUtil;
import com.dearbinge.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品收藏
 *
 * @author wes
 * @date 2019/1/10
 */
@Service("commodityCollectService")
public class CommodityCollectServiceImpl implements CommodityCollectService {

    @Autowired
    private CommodityCollectMapper commodityCollectMapper;
    @Autowired
    private CommodityMessageMapper commodityMessageMapper;

    /**
     * 增加收藏
     *
     * @param collect 收藏
     * @return 结果
     */
    @Override
    public Integer insertCollect(Collect collect) {
        //判断是否已经收藏过了
        String userId = collect.getUserId();
        String commodityId = collect.getCommodityId();
        Collect result = commodityCollectMapper.getCollectIgnoreDelete(userId, commodityId);
        if (result != null) {
            if (result.getDeleted().equals(false)) {
                //收藏过 直接返回1
                return 1;
            } else {
                //收藏过 删除过 重新激活
                result.setDeleted(false);
                return  commodityCollectMapper.updateCollect(result);
            }
        }
        return commodityCollectMapper.insertCollect(collect);
    }



    /**
     * 修改收藏
     *
     * @param collect 收藏
     * @return 结果
     */
    @Override
    public Integer updateCollect(Collect collect) {
        return commodityCollectMapper.updateCollect(collect);
    }

    /**
     * 删除
     *
     * @param id id
     * @return 结果
     */
    @Override
    public Integer deleteCollect(Integer id) {
        return  commodityCollectMapper.deleteCollect(id);
    }


    /**
     * 根据id查询收藏
     *
     * @param id id
     * @return 结果
     */
    @Override
    public Collect getCollectById(Integer id) {
        return commodityCollectMapper.getCollectById(id);
    }

    /**
     * 查询用户的商品收藏
     *
     * @param userId 用户id
     * @return 收藏集
     */
    @Override
    public Page listCollectByUserId(String userId, Integer current, Integer size) {
        size = PageUtils.getDefaultValues(size, 10);
        current = PageUtils.getDefaultValues(current, 1);
        Page page = new Page(current, size);
        List<Collect> list = commodityCollectMapper.listCollectByUserId(userId, page);
        if (list != null) {
            List<CommodityCollectVO> collectList = BeanMapperUtil.map(list, CommodityCollectVO.class);
            //遍历插入商品收藏数 用户
            for (CommodityCollectVO commodityCollectVO : collectList) {
                //插入店铺id 收藏表拿
                CommodityMessage commodity = commodityMessageMapper.getById(commodityCollectVO.getCommodityId());
                if (commodity != null) {
                    CommodityMessageResponseVO commodityMessageResponseVO = new CommodityMessageResponseVO();
                    BeanUtils.copyProperties(commodity,commodityMessageResponseVO);
                    commodityCollectVO.setCommodityMessageResponseVO(commodityMessageResponseVO);
                    //收藏总数
                    Integer collectTotal = commodityCollectMapper.countCollectTotal(commodityCollectVO.getCommodityId(), commodityCollectVO.getShopId());
                    commodityCollectVO.setCollectTotal(collectTotal);
                }
            }
            page.setRecords(collectList);
            Integer total = commodityCollectMapper.countCollectByUserId(userId);
            page.setTotal(total);
        }
        return page;
    }




}